package io.renren.modules.noc.controller;

import io.renren.common.utils.R;
import io.renren.modules.noc.entity.NocMemberEntity;
import io.renren.modules.noc.entity.NocTemplateEntity;
import io.renren.modules.noc.service.NocMemberService;
import io.renren.modules.noc.service.NocTemplateService;
import io.renren.modules.sys.shiro.ShiroUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;
import java.util.List;


/**
 * 群发短信
 *
 * @author whc
 * @email hucheng_86@126.com
 * @date 2019-04-03 22:37:46
 */
@RestController
@RequestMapping("noc/nocsendmsg")
public class NocSendMsgController extends BaseController{

    @Autowired
    private NocMemberService nocMemberService;
    @Autowired
    private NocTemplateService nocTemplateService;

    //群发短信
    @RequestMapping("/send")
    @RequiresPermissions("noc:nocsendmsg:send")
    public R send(@RequestBody NocMemberEntity nocMember, HttpServletRequest request){
        StringBuffer url  = request.getRequestURL();
        String tempContextUrl = url.delete(url.length() - request.getRequestURI().length(), url.length()).append("/").toString();
        //获取展会名称，保存session
        //Integer templateId = this.getDefaultTemplateId();
        Integer templateId = nocMember.getTid();
        //NocTemplateEntity templateEntity = (NocTemplateEntity)ShiroUtils.getSession().getAttribute("template");
        NocTemplateEntity templateEntity = nocTemplateService.getById(templateId);
        if(templateEntity!=null){
            nocMemberService.sendMsg(templateId,nocMember,templateEntity,tempContextUrl);
        }
        return R.ok();
    }

    @RequestMapping("/danfa")
    @RequiresPermissions("noc:nocsendmsg:danfa")
    public R delete(@RequestBody Integer[] ids, HttpServletRequest request){
        StringBuffer url  = request.getRequestURL();
        String tempContextUrl = url.delete(url.length() - request.getRequestURI().length(), url.length()).append("/").toString();
        List<NocMemberEntity> list = (List<NocMemberEntity>)nocMemberService.listByIds(Arrays.asList(ids));
        nocMemberService.sendErcodeMsg(list,tempContextUrl);
        return R.ok();
    }

}
